<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/27/027
 * Time: 18:44
 */

namespace app\Lib\Model\Order;


use app\Lib\Model\DbModel\OrderBindingTag;
use app\Lib\Model\DbModel\OrderInfo;
use app\Lib\Model\DbModel\OrderLog;
use app\Lib\Model\DbModel\OrderOtherThanTime;
use think\Db;

class MonitorOrderLog
{
    /**
     * 监控未回复的
     * @return bool
     * @throws \app\Lib\Exception\BlException
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    public function run(){
        $model = new OrderInfo();
        $log = new OrderLog();
        $array = $model->query("SELECT
	        info.id,info.order_number,
	        max(log.create_time) AS time 
            FROM
	          ab_order_info AS info
            LEFT JOIN ab_order_log AS log ON info.id = log.order_id
            where info.orderStatus = 'underconstruction'
            GROUP BY
	        info.id "
        );
        if(empty($array)){
            return true;
        }
        foreach ($array as $v){
            $model =  new Order($v['order_number']);
            $result = $model->is_hand_stop_word();
//            if($result){
//                return true;
//            }
            if($v['time']<strtotime(date('Y-m-d',time())) && !$result ){
                //执行推送
                $modelOrderPush =new OrderPush($model);
                $modelOrderPush->waitReply();
            }
        }
    }

    /**
     * 已回复工地日志的推送给业主
     * @return bool
     * @throws \app\Lib\Exception\BlException
     */
    public function pushUser(){
        $time   = strtotime(date('Y-m-d',time()));
        $array =Db::query(  "SELECT
	                  info.id,
	                  info.order_number
                    FROM
                    	ab_order_info AS info
                    LEFT JOIN ab_order_log AS log ON info.id = log.order_id
                    WHERE
                    	log.create_time > $time and is_push = 1
                    GROUP BY
                    	info.id");
        if(empty($array)){
            return true;
        }
        foreach ($array as $v){
                $modelOrderPush =new OrderPush(new Order($v['order_number']));
                $modelOrderPush->pushLog();
        }
    }

    /**
     *
     * 工地到时自动转换状态
     * @throws \app\Lib\Exception\BlException
     */
    public function autoStartWord(){
        $model = (new OrderInfo());
        $time = strtotime(date('Y-m-d',time()));
        $map = [
            $model->_orderStatus=>'wait',
            $model->_starting_time=>[['lt',($time+23*3600)],['neq',0],'and'],
        ];
        $orderId = $model->where($map)->column($model->_order_number,$model->_id);
        if($orderId){
            foreach ($orderId as $key=>$v){
                $model->update([$model->_id=>$key,$model->_orderStatus=>'underconstruction']);
                (new Order($v))->delAllCache();
            }
        }
    }

    public function monitorStop(){
        $model = new OrderBindingTag();
        $order_id = $model->where([$model->_status=>1])->column($model->_order_id);

        if(empty($order_id)){
            return true;
        }
        $orderModel = (new OrderInfo());
        $order_number = $orderModel
            ->where([$orderModel->_id=>['in',$order_id]])
            ->column($orderModel->_order_number);
        $timeModel = new OrderOtherThanTime();
        foreach ($order_number as $v){
            $OrderObjModel = new Order($v,false);
            $content = $model->where([$model->_order_id=>$OrderObjModel->get_order_id(),$model->_status=>1])
                ->value($model->_reason);
            $id =  $timeModel
                ->where(
                    [
                        $timeModel->_order_id=>$OrderObjModel->get_order_id(),
                        $timeModel->_create_time=>['gt',strtotime(date('Y-m-d',time()))]
                    ]
                )
                ->value($timeModel->_id);
            if(empty($id)){
                $data = [
                    $timeModel->_order_id=>$OrderObjModel->get_order_id(),
                    $timeModel->_content=>$content,
                    $timeModel->_create_time=>time(),
                    $timeModel->_create_uid=>1,
                ];
                $timeModel->insert($data);
            }
            $orderLog = (new OrderLog());
            $data = [
                $orderLog->_order_id=>$OrderObjModel->get_order_id(),
                $orderLog->_content=>$content,
                $orderLog->_create_time=>time(),
                $orderLog->_create_uid=>1,
                'is_push'=>0
            ];
            $orderLog->insert($data);
        }

    }
}